//
//  SuperviseSearchView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/6.
//

import Foundation
import UIKit

class SuperviseSearchView: UIView {
    let taskItem = SuperviseReportSearchTF(withPlaceholder: "任务名称：")
    let startDateItem = UnarmedSearchItem(withPlaceholder: "开始时间：")
    let endDateItem = UnarmedSearchItem(withPlaceholder: "结束时间：")

    let submitBtn = UIButton.createPrimaryLarge("搜索")

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .bg
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(taskItem)
        taskItem.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp.top).offset(10)
            make.height.equalTo(56)
            make.left.right.equalToSuperview()
        }
        
        addSubview(startDateItem)
        startDateItem.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(taskItem.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview()
        }
        
        addSubview(endDateItem)
        endDateItem.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(startDateItem.snp.bottom)
            make.height.equalTo(56)
            make.left.right.equalToSuperview()
        }
        
        addSubview(submitBtn)
        submitBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(endDateItem.snp.bottom).offset(31)
            make.height.equalTo(50)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth - 20)
        }
    }
}

class SuperviseReportSearchTF: UIView {
    var value: String? {
        set {
            valueTF.text = newValue
        }
        get {
            return valueTF.text
        }
    }
    
    var placeholder: String = "关键字" {
        didSet {
            updatePlaceholder()
        }
    }
    
    let keyL = UILabel()
    let valueTF = UITextField()
    
    init(withPlaceholder _place: String) {
        super.init(frame: .zero)
        self.placeholder = _place
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updatePlaceholder() {
        keyL.text = placeholder
        valueTF.placeholder = "请输入\(placeholder)"
    }
    
    func setupUI() {
        backgroundColor = .white
        
        keyL.text = placeholder
        keyL.textColor = .hex("#666666")
        keyL.font = UIFont.systemFont(ofSize: 16)
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.centerY.equalTo(self.snp.centerY)
            make.left.equalTo(self.snp.left).offset(11)
        }
       
        valueTF.placeholder = "请输入\(placeholder)"
        valueTF.textAlignment = .right
        valueTF.font = UIFont.systemFont(ofSize: 16)
        addSubview(valueTF)
        valueTF.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp.centerY)
            make.left.equalTo(keyL.snp.right).offset(10)
            make.right.equalToSuperview().offset(-20)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#F3F3F3")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(0.5)
        }
    }
}
